import tkinter as tk
import requests

def send_request():
    user_input = input_box.get("1.0", "end-1c")  # 获取文本输入框的内容
    payload["messages"][0]["content"] = user_input  # 更新请求参数

    response = requests.post(url, headers=headers, json=payload)

    if response.status_code == 200:
        result = response.json()
        output_box.delete("1.0", tk.END)  # 清空结果显示框
        output_box.insert(tk.END, result["choices"][0]["message"]["content"])  # 显示content
    else:
        output_box.delete("1.0", tk.END)
        output_box.insert(tk.END, f"Request failed with status code: {response.status_code}")

# 请求网址
url = "https://open.bigmodel.cn/api/paas/v4/chat/completions"

# 请求头
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer be5652da760c07a21de7bb18b068172c.9jylW1rT8w8QWp6M"
}

# 接口请求参数
payload = {
    "model": "glm-4",
    "messages": [{"role": "user", "content": ""}]  # 初始化为空字符串
}

# 创建主窗口
root = tk.Tk()
root.title("提问系统")

# 输入框
input_label = tk.Label(root, text="请输入你的问题:")
input_label.pack()
input_box = tk.Text(root, width=40, height=5)
input_box.pack()

# 结果显示框
output_label = tk.Label(root, text="回答:")
output_label.pack()
output_box = tk.Text(root, width=40, height=5)
output_box.pack()

# 提问按钮
button = tk.Button(root, text="提问", command=send_request)
button.pack()

# 运行主循环
root.mainloop()